package com.jinyou.chengfengshop.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jinyou.chengfengshop.R;
import com.jinyou.chengfengshop.bean.MessageBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 消息列表适配器
 * Created by zhuzhiqiang on 2018/9/26.
 */

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.Viewholder> {
    private List<MessageBean> mb;
    private Context Mcontext;

    public MessageAdapter(List<MessageBean> mb, Context mcontext) {
        this.mb = mb;
        Mcontext = mcontext;
    }

    @Override
    public Viewholder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message, parent, false);
        Viewholder vh = new Viewholder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(Viewholder holder, int position) {
        String time = formatData("yyyy-MM-dd HH:mm:ss", mb.get(position).getCreateTim());
        holder.comment_tv.setText(mb.get(position).getComment());
        holder.time_tv.setText(time);
        holder.toName_tv.setText(mb.get(position).getToName());
    }

    @Override
    public int getItemCount() {
        return mb.size();
    }

    public class Viewholder extends RecyclerView.ViewHolder{
        private TextView comment_tv;
        private TextView toName_tv;
        private TextView time_tv;
        public Viewholder(View itemView) {
            super(itemView);
            comment_tv = (TextView) itemView.findViewById(R.id.comment_tv);
            toName_tv = (TextView) itemView.findViewById(R.id.toName_tv);
            time_tv = (TextView) itemView.findViewById(R.id.time_tv);
        }
    }


    private String formatData(String dataFormat, long timeStamp) {
        if (timeStamp == 0) {
            return "";
        }

        String mm = timeStamp+"";
        mm = mm.substring(0,10);
        timeStamp = Long.parseLong(mm);

        timeStamp = timeStamp * 1000;
        String result = "";
        SimpleDateFormat format = new SimpleDateFormat(dataFormat);
        result = format.format(new Date(timeStamp));
        return result;
    }
}
